package com.zablert.lazyutil.drawable;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.zablert.lazyutil.utils.Log;
import org.apache.commons.lang.StringUtils;

/**
 * VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
 * Log.d("virtualFile1" + virtualFile1);
 * <p>
 * // 获取多个 VirtualFile
 * VirtualFile[] virtualFiles = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
 * // 方式二: 从本地文件系统路径获取
 * VirtualFile virtualFileFromLocalFileSystem = LocalFileSystem.getInstance().findFileByIoFile(new File("path"));
 * // 方式三: 从 PSI 文件 (如果 PSI 文件仅存在内存中, 则可能返回 null)
 * PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
 * if (psiFile != null) {
 * psiFile.getVirtualFile();
 * }
 * // 方式四: 从 document 中获取
 * Document document = Objects.requireNonNull(e.getData(PlatformDataKeys.EDITOR)).getDocument();
 * VirtualFile virtualFileFromDocument = FileDocumentManager.getInstance().getFile(document);
 **/
public class ShapeGenerateAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
//        // 获取 VirtualFile 方式一:
        VirtualFile virtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE);
//        Project project = e.getProject();
////        VirtualFile virtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE);
        String mPath = "";
        if (virtualFile.isDirectory()) {
            mPath = virtualFile.getPath();
        }else {
            mPath = virtualFile.getParent().getPath();
        }
//        Module module = ModuleUtil.findModuleForFile(virtualFile, project);

//        String moduleRootPath = ModuleRootManager.getInstance(module).getContentRoots()[0].getPath();
//        String actionDir = virtualFile.getPath();
//        String str = StringUtils.substringAfter(actionDir, moduleRootPath + "/src/main/java/");
        //获取右键后的路径
//        String basePackage = StringUtils.replace(str, "/", ".");
        ShapeGeneratePage page = new ShapeGeneratePage();
        page.setPath(mPath);
        page.show();

    }


}
